//
//  FootCustCellView.m
//  GomeHome
//
//  Created by UNOHACHA on 16/12/9.
//  Copyright © 2016年 iMacOne. All rights reserved.
//

#import "FootCustCellView.h"
#import "UIView+JDExtension.h"
#import "FootCustomCell.h"
#import "FootCustModel.h"
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@implementation FootCustCellView   
{
    UITableView * _selfTableView;
    
//    NSArray * _items;
    NSMutableArray * _startArr;
    NSInteger  _selectIndex;
    
    NSString  * selecIsOpen;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        _selectIndex = -1;
        
        
        _startArr  = [[NSMutableArray alloc]initWithCapacity:0];

        NSArray * _items = @[@{@"title":@"馆藏",@"items":@[@"",@""],@"isOpen":@"NO"},
          @{@"title":@"学院",@"items":@[@"",@""],@"isOpen":@"NO"},
          @{@"title":@"艺术家",@"items":@[@"",@""],@"isOpen":@"NO"},
          @{@"title":@"艺术专业",@"items":@[@"",@""],@"isOpen":@"NO"}
          ];
        for (NSDictionary * dic in _items) {
            FootCustModel * footMo  =[[FootCustModel alloc]initWithDictionary:dic];
            [_startArr addObject:footMo];
        }
        
        
        _selfTableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStyleGrouped];
//        _selfTableView.backgroundColor = [UIColor redColor] ;
        _selfTableView.delegate = self;
        _selfTableView.dataSource = self;
        _selfTableView.bounces = NO;
        _selfTableView.scrollsToTop = NO;
        _selfTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _selfTableView.showsVerticalScrollIndicator = NO;
        [self addSubview:_selfTableView];
    }
    return self;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    FootCustModel * footMo = _startArr[section];
    if ([footMo.isOpen isEqualToString:@"NO"]) {
        return 0;
    }else {
        return 1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _startArr.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    FootCustModel * footMo  =  _startArr[section];
    UIButton * bgView = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44)];
    bgView.tag = section +200;
    [bgView addTarget:self action:@selector(chosIndex:) forControlEvents:UIControlEventTouchUpInside];
    bgView.backgroundColor = UIColorFromRGB(0x2b393f);
    
    UILabel * titL=  [[UILabel alloc]initWithFrame:CGRectMake(20, 0, bgView.bounds.size.width, 44)];
    titL.textColor = [UIColor whiteColor];
    titL.text = footMo.title;
    titL.font = [UIFont systemFontOfSize:14];
    [bgView addSubview:titL];
    
    UIView * linView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, bgView.bounds.size.width, 0.5)];
    linView.backgroundColor = UIColorFromRGB(0x14252c);
    [bgView addSubview:linView];

    
    return bgView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 88;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FootCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LeftVCTableCell"];
    if (cell == nil) {
        cell = [[FootCustomCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"LeftVCTableCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return cell;
}

- (void)chosIndex:(UIButton *)sender {
    NSInteger index = sender.tag-200;

    for (NSInteger i = 0; i<_startArr.count; i++) {
            FootCustModel * footMo = _startArr[i];
        if (i == index) {
            if ([footMo.isOpen isEqualToString:@"NO"]) {
                footMo.isOpen = @"YES";
            }else {
                footMo.isOpen = @"NO";
            }
        }else {
            footMo.isOpen = @"NO";
        }
    }
    
      
    CGFloat height = 0;
    for ( FootCustModel * footMo in _startArr) {
        if ([footMo.isOpen isEqualToString:@"YES"]) {
            height = 88;
            _selfTableView.height =_selfTableView.height+height;
            if (_foBlock) {
                _foBlock(height);
            }
            [_selfTableView reloadData];
            return;
        }
    }
    height = 0;
    
    _selfTableView.height =_selfTableView.height+height;
    if (_foBlock) {
        _foBlock(height);
    }

    [_selfTableView reloadData];
}





@end
